<script setup>
import PageContainer from "@/components/PageContainer.vue";
import { ref, onMounted } from 'vue'
import { Edit, Delete } from "@element-plus/icons-vue";
import { artDelChannelService, artGetChannelService } from "@/api/article.js";
import ChannelEdit from "@/views/article/components/ChannelEdit.vue";

const channelList = ref([])
const dialog = ref()
const getChannelList = async () => {
  const res = await artGetChannelService()
  channelList.value = res.data.data
  //console.log(channelList.value)
}

onMounted(() => {
  // 页面渲染后发请求获取数据
  getChannelList()
})

const onEditChannel = (row) => {
  dialog.value.open(row)
  //console.log(row, $index)
}

const onDelChannel = async (row) => {
  await ElMessageBox.confirm('你确定要删除该分类吗', '温馨提示', {
    type: 'warning',
    confirmButtonText: '确认',
    cancelButtonText: '取消'
  })
  const res = await artDelChannelService(row.id)
  if(res.data.code === 0) {
    ElMessage.success('删除成功')
    getChannelList()
  } else {
    ElMessage.error('删除失败')
  }
}

const onAddChannel = () => {
  dialog.value.open({})
}

const onSuccess = () => {
  getChannelList()
}
</script>
<template>
  <page-container title="文章分类">
    <template #extra>
      <el-button type="primary" @click="onAddChannel">添加分类</el-button>
    </template>

    <!--   下面是主体部分   -->
    <el-table :data="channelList" style="width: 100%;">
      <el-table-column label="序号" type="index" width="100"></el-table-column>
      <el-table-column prop="cate_name" label="分类名称"></el-table-column>
      <el-table-column prop="cate_alias" label="分类别名"></el-table-column>
      <el-table-column label="操作" width="150">
        <!-- 作用域插槽 -->
        <!-- row 就是 channelList 中的一项， $index 下标  Edit 是Element-plue 中的图标   circle 图标为圆形 plain 图标样式-->
        <template #default="{ row, $index }">
          <el-button :icon="Edit" circle plain type="primary" @click="onEditChannel(row, $index)"></el-button>
          <el-button :icon="Delete" circle plain type="danger" @click="onDelChannel(row, $index)"></el-button>
        </template>
      </el-table-column>

      <template #empty>
        <el-empty description="没有数据"></el-empty>
      </template>

    </el-table>
    <!--    组件已封装
    <el-dialog v-model="dialogVisible" title="添加弹层" width="30%">
      <span></span>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="dialogVisible = false">确认</el-button>
        </span>
      </template>
    </el-dialog>  -->

    <channel-edit ref="dialog" @success="onSuccess"></channel-edit>
  </page-container>
</template>
<style lang="scss" scoped>
</style>